import wsgiref.handlers
import os
import random
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp import template
import urllib

class Banner(db.Model):
  name = db.StringProperty()
  ad_code = db.TextProperty()
  rand = db.FloatProperty(default=0.0)
  
class MainPage(webapp.RequestHandler):
  def get(self):
    template_values = {
          'version': "app_version",
          'message': ""
    }  
    path = os.path.join(os.path.dirname(__file__), 'admin.html')
    self.response.out.write(template.render(path, template_values))
  
  def post(self):
    b = uzdb.Banner()
    b.name = self.request.get('name')
    b.ad_code = self.request.get('ad_code')
    b.rand = random.random()
    b.put()
    template_values = {
          'version': "app_version",
          'message': "added!"
    }  
    path = os.path.join(os.path.dirname(__file__), 'admin.html')
    self.response.out.write(template.render(path, template_values))
  

def main():
  application = webapp.WSGIApplication(
                                       [
                                        ('/.*', MainPage)],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)